package com.ruoyi.product.service.impl;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.product.domain.Mycomment;
import com.ruoyi.product.enums.CommentType;
import com.ruoyi.product.service.IMycommentService;
import com.ruoyi.product.service.QuanZiApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.ruoyi.product.mapper.ProductCommentMapper;
import com.ruoyi.product.domain.ProductComment;
import com.ruoyi.product.service.IProductCommentService;

/**
 * 用户评论表Service业务层处理
 *
 * @author 林道敬
 * @date 2025-01-07
 */
@Service
public class ProductCommentServiceImpl extends ServiceImpl<ProductCommentMapper, ProductComment> implements IProductCommentService
{
    @Autowired
    private ProductCommentMapper productCommentMapper;
    @Autowired
    private QuanZiApi quanZiApi;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private IMycommentService mycommentService;
    @Override
    public Boolean likeComment(Long userId, String publishId) {
        //判断该⽤户是否已经点赞，如果已经点赞就直接返回
        if (quanZiApi.queryUserIsLike(userId, publishId)) {
            return false;
        }

        //保存Comment数据
        Boolean result = quanZiApi.saveLikeComment(userId, publishId);
        if (!result) {
            return false;
        }


        //修改点赞数
        String redisKey = quanZiApi.getCommentRedisKeyPrefix(publishId);
        String hashKey = CommentType.LIKE.toString();
        this.redisTemplate.opsForHash().increment(redisKey, hashKey, 1);

        //⽤户是否点赞
        String userHashKey = quanZiApi.getCommentUserLikeRedisKeyPrefix(userId);
        this.redisTemplate.opsForHash().put(redisKey, userHashKey, "1");



        //总点赞数
        Long count = quanZiApi.queryLikeCount(publishId);
        QueryWrapper<Mycomment> mycommentQueryWrapper = new QueryWrapper<>();
        mycommentQueryWrapper.eq("product_id",publishId);
        Mycomment one = mycommentService.getOne(mycommentQueryWrapper);
        one.setLikeNum(count);
        //标识评论点赞
        one.setIsLiked(false);
        mycommentService.saveOrUpdate(one);
        return true;
    }


    @Override
    public Boolean disLikeComment(Long userId, String publishId) {
        //判断⽤户是否已经点赞，如果没有点赞就返回
        if (!quanZiApi.queryUserIsLike(userId, publishId)) {
            return false;
        }
        //删除评论数据
        Boolean result = quanZiApi.removeComment(userId, publishId,
                CommentType.LIKE.getType());
        if (!result) {
            return false;
        }

        //修改Redis中的数据

        //修改点赞数
        String redisKey = quanZiApi.getCommentRedisKeyPrefix(publishId);
        String hashKey = CommentType.LIKE.toString();
        this.redisTemplate.opsForHash().increment(redisKey, hashKey, -1);

        //⽤户是否点赞
        String userHashKey = quanZiApi.getCommentUserLikeRedisKeyPrefix(userId);
        this.redisTemplate.opsForHash().delete(redisKey, userHashKey);

        //总点赞数
        Long count = quanZiApi.queryLikeCount(publishId);
        QueryWrapper<Mycomment> mycommentQueryWrapper = new QueryWrapper<>();
        mycommentQueryWrapper.eq("product_id",publishId);
        Mycomment one = mycommentService.getOne(mycommentQueryWrapper);
        //标识评论取消点赞
        one.setIsLiked(true);
        one.setLikeNum(count);
        mycommentService.saveOrUpdate(one);
        return true;
    }
    @Override
    public Boolean loveComment(Long userId, String publishId) {
        //查询该⽤户是否已经喜欢
        if (quanZiApi.queryUserIsLove(userId, publishId)) {
            return false;
        }

        //喜欢
        boolean result = quanZiApi.saveLoveComment(userId, publishId);
        if (!result) {
            return false;
        }

        //喜欢成功后，修改Redis中的总的喜欢数
        String redisKey = quanZiApi.getCommentRedisKeyPrefix(publishId);
        String hashKey = CommentType.LOVE.toString();
        this.redisTemplate.opsForHash().increment(redisKey, hashKey, 1);

        //标记⽤户已经喜欢
        hashKey = quanZiApi.getCommentUserLoveRedisKey(userId);
        this.redisTemplate.opsForHash().put(redisKey, hashKey, "1");


        //总喜欢数
        Long count = quanZiApi.queryLoveCount(publishId);
        QueryWrapper<Mycomment> mycommentQueryWrapper = new QueryWrapper<>();
        mycommentQueryWrapper.eq("product_id",publishId);
        Mycomment one = mycommentService.getOne(mycommentQueryWrapper);
        one.setLoveNum(count);
        one.setIsLoved(false);
        mycommentService.saveOrUpdate(one);
        return true;
    }
    @Override
    public Boolean disLoveComment(Long userId, String publishId) {
        if (!quanZiApi.queryUserIsLove(userId, publishId)) {
            //如果⽤户没有喜欢，就直接返回
            return false;
        }

        boolean result = quanZiApi.removeComment(userId, publishId,
                CommentType.LOVE.getType());
        if (!result) {
            //删除失败
            return false;
        }

        //删除redis中的记录
        String redisKey = quanZiApi.getCommentRedisKeyPrefix(publishId);
        String hashKey = quanZiApi.getCommentUserLoveRedisKey(userId);
        this.redisTemplate.opsForHash().delete(redisKey, hashKey);
        this.redisTemplate.opsForHash().increment(redisKey,
                CommentType.LOVE.toString(), -1);
        //总喜欢数
        Long count = quanZiApi.queryLoveCount(publishId);
        QueryWrapper<Mycomment> mycommentQueryWrapper = new QueryWrapper<>();
        mycommentQueryWrapper.eq("product_id",publishId);
        Mycomment one = mycommentService.getOne(mycommentQueryWrapper);
        one.setLoveNum(count);
        one.setIsLoved(true);
        mycommentService.saveOrUpdate(one);
        return true;
    }
    /**
     * 查询用户评论表
     *
     * @param id 用户评论表主键
     * @return 用户评论表
     */
    @Override
    public ProductComment selectProductCommentById(Long id)
    {
        return productCommentMapper.selectProductCommentById(id);
    }

    /**
     * 查询用户评论表列表
     *
     * @param productComment 用户评论表
     * @return 用户评论表
     */
    @Override
    public List<ProductComment> selectProductCommentList(ProductComment productComment)
    {
        return productCommentMapper.selectProductCommentList(productComment);
    }

    /**
     * 新增用户评论表
     *
     * @param productComment 用户评论表
     * @return 结果
     */
    @Override
    public int insertProductComment(ProductComment productComment)
    {
        return productCommentMapper.insertProductComment(productComment);
    }

    /**
     * 修改用户评论表
     *
     * @param productComment 用户评论表
     * @return 结果
     */
    @Override
    public int updateProductComment(ProductComment productComment)
    {
        return productCommentMapper.updateProductComment(productComment);
    }

    /**
     * 批量删除用户评论表
     *
     * @param ids 需要删除的用户评论表主键
     * @return 结果
     */
    @Override
    public int deleteProductCommentByIds(Long[] ids)
    {
        return productCommentMapper.deleteProductCommentByIds(ids);
    }

    /**
     * 删除用户评论表信息
     *
     * @param id 用户评论表主键
     * @return 结果
     */
    @Override
    public int deleteProductCommentById(Long id)
    {
        return productCommentMapper.deleteProductCommentById(id);
    }
}
